//
//  CJExten+Array.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-06-05.
//

import Foundation

public extension Array where Element == UInt8 {
    
    /// 转16进制字符串
    func cj_hexString(space: Bool = false) -> String {
        let hex = self.map { byte -> String in
            return String(format: "%02X", byte)
        }.joined(separator: space ? " " : "")
        return hex
    }
    
    /// 转2进制字符串
    func cj_binaryString(space: Bool = false) -> String {
        let binary = self.map { byte -> String in
            return String(format: "%08u", byte)
        }.joined(separator: space ? " " : "")
        return binary
    }
    
}

public extension Array {
    
    /// 安全的取数据
    func cj_sub(loc: Int, len: Int) -> Array {
        let start = loc
        let end = loc + len
        if end >= self.count {
            return []
        }
        return Array(self[start...end])
    }
    
    /// 安全的取数据
    func cj_sub(start: Int, end: Int) -> Array {
        let start = start
        let end = end
        if end >= self.count {
            return []
        }
        return Array(self[start...end])
    }
    
}
